/**
 * Copyright (C) 2018-2019
 * All rights reserved, Designed By www.joolun.com
 * 注意：
 * 本软件为www.joolun.com开发研制，项目使用请保留此说明
 */
package com.joolun.web.api;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.joolun.common.core.domain.AjaxResult;
import com.joolun.mall.entity.Apply;
import com.joolun.mall.service.ApplyService;
import com.joolun.weixin.service.WxUserService;
import com.joolun.weixin.utils.ThirdSessionHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * 报名志愿
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/weixin/api/ma/apply")
@Api(value = "apply", tags = "报名志愿")
public class ApplyApi {

	@Autowired
	private ApplyService applyService;
	@Autowired
	private WxUserService wxUserService;

	/**
	 * 分页查询报名
	 * @param page 分页对象
	 * @param entity 报名
	 * @return
	 */
	@ApiOperation(value = "根据用户id分页查询报名")
	@GetMapping("/getApplyByPage")
	public AjaxResult getApplyByPage(Page page, Apply entity) {
		entity.setUserId(ThirdSessionHolder.getWxUserId());
		return AjaxResult.success(applyService.page1(page, entity));
	}

    /**
    * 通过id查询报名详情
    * @param id
    * @return R
    */
	@ApiOperation(value = "通过id查询报名详情")
    @GetMapping("/{id}")
    public AjaxResult getById(@PathVariable("id") String id){
		Apply wish = applyService.getById(id);
		// 判断是否已报名
//		if(StringUtils.isNotEmpty(ThirdSessionHolder.getWxUserId())){
//			LambdaQueryWrapper<Apply> qw = Wrappers.<Apply>lambdaQuery();
//			qw.eq(Apply::getUserId,ThirdSessionHolder.getWxUserId());
//			Apply apply = applyService.getOne(qw);
//			if(apply != null){
//				wish.setSignFlag(1);
//			}else{
//				wish.setSignFlag(2);
//			}
//		}
        return AjaxResult.success(wish);
    }

	@ApiOperation(value = "新增志愿报名")
	@PostMapping("addApply")
	public AjaxResult save(@RequestBody Apply entity){
		entity.setCreateTime(LocalDateTime.now());
		entity.setUserId(ThirdSessionHolder.getWxUserId());
		return AjaxResult.success(applyService.save(entity));
	}


}
